/** PROGETTO PRAISED **/
package it.uniroma3.dia.praised.util;

import it.uniroma3.dia.praised.brenda.model.Enzyme;
import it.uniroma3.dia.praised.brenda.model.Literature;
import it.uniroma3.dia.praised.brenda.model.Synonym;
import it.uniroma3.dia.praised.exception.CrawlerException;
import it.uniroma3.dia.praised.pubmed.model.Article;
import it.uniroma3.dia.praised.pubmed.model.Section;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.Writer;
import java.nio.charset.Charset;

import com.thoughtworks.xstream.XStream;

/**
 * SerializzaXML : Classe che effettua la serializzazione delle informazioni di interesse, creando opportuni file xml
 * 
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 * @version 1.0
 */
public class SerializeXML implements Serializable {

	private static final long serialVersionUID = 5683401735116810228L;
	
	private static final String PATH = "repository/xml/";
	private static final String EXTENSION = ".xml";
	private static final String ENCODING = "UTF-8";
	
	private Enzyme enzyme;
	private Article article;


	public SerializeXML(Enzyme enzyme) {
		this.enzyme = enzyme;
	}
	
	public SerializeXML(Article article) {
		this.article = article;
	}

	/**
	 * This method create an XML file for each <code>Enzyme</code>
	 * 
	 * @return <code>true</code> - if the file was created correctly.
	 * @throws CrawlerException
	 */
	public boolean serializeEnzyme() throws CrawlerException {
		
		XStream xStream = new XStream();
		xStream.autodetectAnnotations(true);
		xStream.processAnnotations(new Class<?>[] {Enzyme.class, Synonym.class, Literature.class});
		try {
			OutputStream outputStream = new FileOutputStream(PATH + enzyme.getID() + EXTENSION);
			Writer writer = new OutputStreamWriter(outputStream, Charset.forName(ENCODING));
			writer.write("<?xml version=\"1.0\" encoding=\"" + ENCODING + "\" ?>\n");
			writer.write("<!DOCTYPE enzyme SYSTEM \"../enzyme.dtd\">\n");
			xStream.toXML(this.enzyme, writer); 
			
		} catch (FileNotFoundException e) {
			throw new CrawlerException(e);
		} catch (Exception e) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * This method create an XML file for each <code>Article</code>
	 * 
	 * @return <code>true</code> - if the file was created correctly.
	 * @throws CrawlerException
	 */
	public boolean serializeArticle() throws CrawlerException {
		
		XStream xStream = new XStream();
		xStream.autodetectAnnotations(true);
		xStream.processAnnotations(new Class<?>[] {Article.class, Section.class});
		try {
			OutputStream outputStream = new FileOutputStream(PATH + article.getPMCID() + EXTENSION);
			Writer writer = new OutputStreamWriter(outputStream, Charset.forName(ENCODING));
			writer.write("<?xml version=\"1.0\" encoding=\"" + ENCODING + "\" ?>\n");
			xStream.toXML(this.article, writer); 
			
		} catch (FileNotFoundException e) {
			throw new CrawlerException(e);
		} catch (Exception e) {
			return false;
		}
		
		return true;
		
	}
	
}
